const app = getApp();
Page({
    data:{
        data: [],
        page:1,
        loadData:false,
        reg    : false,
        isInfo : false
    },
    onLoad(){
        this.getData(this.data.page)
    },
    onPullDownRefresh (){
        this.setData({
            page:1,
            data:[]
        })
        this.getData(this.data.page)
        wx.stopPullDownRefresh()
    },
    onReachBottom(){
        this.setData({
            page:this.data.page+1
        })
        this.getData(this.data.page)
    },
    getData(page){
        if (!this.data.loadData){
            this.setData({
                loadData:true
            })
            app.axios('/works','GET',response=>{
                if(response){
                    this.setData({
                        data: [...this.data.data,...response]
                    })
                }
                this.setData({
                    loadData:false
                })
            },{page:page})
        }

    },
    details(e){
        if (!this.data.isInfo){
            this.getInfo()
        }
        if (!this.data.reg){
            wx.showModal({
                title: '错误',
                content: '您未完成班组注册，请先注册~',
                showCancel: false
            })
        }else {
            wx.navigateTo({
                url: '/pages/pDetails/index'+'?id='+e.currentTarget.dataset.id
            })
        }
    },
    switchMap(){
        wx.redirectTo({
            url: '/pages/mapProject/index'
        })
    },
    getInfo(){
        wx.getUserInfo({
            success:res=>{
                // console.log(res)
                app.axios('/info','PUT',response=>{
                    if (response.infoed){
                        this.setData({
                            reg    : !!response.reg,
                            isInfo : true
                        })
                    }
                    wx.redirectTo({
                        url: '/pages/work/index'
                    })
                },res)
            },
            fail:res=>{
                // console.log(res)
            }
        })
    },
});
